package com.dnndo.common.core.domain;

import com.github.pagehelper.PageInfo;
import com.dnndo.common.constant.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

@Data
@ApiModel(value = "分页数据")
public class PageResult<T> {
    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final int FAIL = HttpStatus.ERROR;

    @ApiModelProperty(value = "列表数据")
    private List<T> rows;
    @ApiModelProperty(value = "总条数")
    private long total;
    @ApiModelProperty(value = "状态码")
    private int code;
    @ApiModelProperty(value = "消息")
    private String msg;

    public PageResult() {
    }

    public PageResult(List<T> data) {
        this.rows = data;
        this.total = new PageInfo<T>(data).getTotal();
    }
    public static <T> PageResult<T> fail()
    {
        return restResult(null, FAIL, "操作失败",0);
    }
    public static <T> PageResult<T> fail(String msg)
    {
        return restResult(null, FAIL, msg,0);
    }
    public static <T> PageResult<T> ok(List<T> data)
    {
        return restResult(data, SUCCESS, "操作成功",new PageInfo<>(data).getTotal());
    }
    public static <T> PageResult<T> pageOk(List<T> data,long total)
    {
        return restResult(data, SUCCESS, "操作成功",total);
    }


    private static <T> PageResult<T> restResult(List<T> data, int code, String msg,long total)
    {
        PageResult<T> apiResult = new PageResult<>();
        apiResult.setCode(code);
        apiResult.setRows(data);
        apiResult.setMsg(msg);
        apiResult.setTotal(total);

        return apiResult;
    }
}
